package com.example.jpatest.controller.restful;

import com.example.jpatest.entity.Teacher;
import com.example.jpatest.entity.Team;
import com.example.jpatest.entity.TeamTeacher;
import com.example.jpatest.exception.teacherException.TeacherExistException;
import com.example.jpatest.exception.teacherException.TeacherNotExistException;
import com.example.jpatest.exception.teamException.HasInTeamException;
import com.example.jpatest.exception.teamException.NotInTeamException;
import com.example.jpatest.exception.teamException.TeamNotExistException;
import com.example.jpatest.service.impl.TeacherServiceImpl;
import com.example.jpatest.service.impl.TeamServiceImpl;
import com.example.jpatest.unit.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TeacherController {
    @Autowired
    TeacherServiceImpl teacherService;

    @Autowired
    TeamServiceImpl teamService;

    /**
     * 获得所有老师的信息
     */
    @GetMapping("/teachers")
    public List<Teacher> teachers() {
        return teacherService.getTeachers();
    }

    /**
     * 添加老师
     */
    @PostMapping("/teacher")
    public Result addTeacher(@RequestBody Teacher teacher) {
        if (teacherService.getById(teacher.getId()) != null) {
            throw new TeacherExistException();
        }
        teacherService.saveTeacher(teacher);
        return Result.succMsg();
    }

    /**
     * 更新老师信息
     */
    @PutMapping("/teacher")
    public Result updateTeacher(@RequestBody Teacher teacher) {
        if (teacherService.getById(teacher.getId()) == null) {
            throw new TeacherNotExistException();
        }
        teacherService.saveTeacher(teacher);
        return Result.succMsg();
    }

    /**
     * 根据工号获得老师信息
     */
    @GetMapping("/teacher/{id}")
    public Result getTeacher(@PathVariable Long id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher == null) {
            throw new TeacherNotExistException();
        }
        return Result.succ(teacher);
    }

    /**
     * 老师加入队伍
     */
    @PostMapping("/teacher/{teacherId}/team")
    public Result joinTeam(
            @PathVariable Long teacherId,
            @RequestParam(name = "teamName") String teamName,
            @RequestParam(name = "teacherLevel") TeamTeacher.TeacherLevel teacherLevel) {
        Teacher teacher = teacherService.getById(teacherId);
        Team team = teamService.getByName(teamName);
        if (teacher == null) {
            throw new TeacherNotExistException();
        }
        if (team == null) {
            throw new TeamNotExistException();
        }
        if (teacherService.isInTeam(teacher, team)) {
            throw new HasInTeamException();
        }
        teacherService.joinTeam(teacher, team, teacherLevel);
        return Result.succMsg();
    }

    /**
     * 查找老师中所有队伍
     */
    @GetMapping("/teacher/{id}/teams")
    public Result getTeams(@PathVariable Long id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher == null) {
            throw new TeacherNotExistException();
        }
        return Result.succ(teacherService.getTeams(teacher));
    }

    /**
     * 老师退出队伍
     */
    @DeleteMapping("/teacher/{teacherId}/team")
    public Result teacherTeams(
            @PathVariable Long teacherId,
            @RequestParam(name = "teamName") String teamName) {
        Teacher teacher = teacherService.getById(teacherId);
        Team team = teamService.getByName(teamName);
        if (teacher == null) {
            throw new TeacherNotExistException();
        }
        if (team == null) {
            throw new TeamNotExistException();
        }
        if (!teacherService.isInTeam(teacher, team)) {
            throw new NotInTeamException();
        }
        teacherService.quitTeam(teacher,team);
        return Result.succMsg();
    }

    /**
     * 获得老师等级
     */
    @GetMapping("/teacher/{teacherId}/teacherLevel")
    public Result getTeacherLevel(
            @PathVariable Long teacherId,
            @RequestParam(name = "teamName") String teamName) {
        Teacher teacher = teacherService.getById(teacherId);
        Team team = teamService.getByName(teamName);
        if (teacher == null) {
            throw new TeacherNotExistException();
        }
        if (team == null) {
            throw new TeamNotExistException();
        }
        if (!teacherService.isInTeam(teacher, team)) {
            throw new NotInTeamException();
        }
        return Result.succ(teacherService.getTeacherLevel(teacher, team));
    }

    /**
     * 根据工号删除老师
     */
    @DeleteMapping("/teacher/{id}")
    public Result deleteTeacher(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        if (teacher == null) {
            throw new TeacherNotExistException();
        }
        teacherService.deleteTeacher(teacher);
        return Result.succMsg();
    }

    /**
     * 获得老师所有报名
     */
    @GetMapping("/teacher/{id}/enters")
    public Result GetEnters(@PathVariable Long id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher == null) {
            throw new TeacherNotExistException();
        }
        return Result.succ(teacherService.getEnters(teacher));
    }
}
